﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using SuffixTrie;

namespace SuffixTrie
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        SuffixTree suffixtree = new SuffixTree();

        public MainWindow()
        {
            InitializeComponent();

            initTree();
        }

        public void initTree()
        {
            string file = "D:\\Code\\Piny\\dict2.txt";
            StreamReader sr = new StreamReader(file, Encoding.UTF8);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Length <= 0)
                    continue;
                string[] vs = line.Split('\t');
                suffixtree.AddNode(vs[1]);
            }

            foreach (SuffixNode c in suffixtree.Root.Children)
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = c.Text + "(" + c.Frequentness.ToString() + ")";
                tree.Items.Add(item);

                foreach (SuffixNode n in c.Children)
                {
                    initTree(ref item, n);
                }
            }
        }

        public void initTree(ref TreeViewItem parent, SuffixNode node)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = node.Text + "(" + node.Frequentness.ToString() + ")";

            parent.Items.Add(item);

            foreach (SuffixNode n in node.Children)
            {
                initTree(ref item, n);
            }
        }

        private void search_Click(object sender, RoutedEventArgs e)
        {
            string text = searchtext.Text;
            int ret = suffixtree.SearchText(text);
            MessageBox.Show("共存在 " + ret.ToString() + " 个词组");
        }

        private void presearch_Click(object sender, RoutedEventArgs e)
        {
            string text = searchtext.Text;
            string[] rets = suffixtree.SearchPrefixText(text);

            string show = "共找到" + rets.Length.ToString() + "个相同前缀的词组：\n";
            foreach (string s in rets)
                show = show + s + "\n";

            MessageBox.Show(show);
        }
    }
}
